package qr_code

import (
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/weixin"

	"github.com/gin-gonic/gin"
)

func ShareQRCode(c *gin.Context) {

	var req struct {
		Scene      string `binding:"max=32"`
		EnvVersion string `binding:"required,oneof=release trial develop"`
		IsHyaline  bool
		CheckPath  bool
		Page       string `binding:"required,max=255"`
		Width      uint   `binding:"required,min=280,max=1280"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	var qr weixin.QRCodeGetReq
	qr.Scene = req.Scene
	qr.EnvVersion = req.EnvVersion
	qr.IsHyaline = req.IsHyaline
	qr.Page = req.Page
	qr.Width = req.Width
	qr.CheckPath = req.CheckPath

	var by, err = qr.QRCodeGet()
	if err != nil {
		c.String(500, err.Error())
		bean.Log.Warning(err.Error())
		return
	}

	c.Header("Content-Type", "image/jpeg")
	c.Header("Content-Disposition", "attachment; filename=\"share_qr_code.jpeg\"")

	c.Writer.Write(by)
}
